package com.example.myapplication;

import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.widget.FrameLayout;

import androidx.appcompat.app.AppCompatActivity;

import com.github.lzyzsd.jsbridge.BridgeHandler;
import com.github.lzyzsd.jsbridge.BridgeWebView;
import com.github.lzyzsd.jsbridge.CallBackFunction;
import com.github.lzyzsd.jsbridge.MainJavascriptInterface;
import com.tencent.smtt.export.external.interfaces.IX5WebChromeClient;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

public class MainActivity extends AppCompatActivity {

    private BridgeWebView webView;
    private FrameLayout container;
    private WebChromeClient.CustomViewCallback mCallback;
    private WindowManager windowManager;
    private View fullScreenLayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 如果之前处于全屏状态，重新进入后需要再次调用全屏
        if (fullScreenLayer != null) {
            fullScreen(fullScreenLayer);
        }
    }

    public void init() {
        container = findViewById(R.id.container);
        webView = (BridgeWebView) findViewById(R.id.webView);
        //获取WebSettings类的实例，此类用于对WebView加载的网页进行设置
        com.tencent.smtt.sdk.WebSettings settings = webView.getSettings();
        //使WebView可以使用JavaScript
        settings.setLoadsImagesAutomatically(true);
        // 本地 DOM 存储（解决加载某些网页出现白板现象）
        settings.setDomStorageEnabled(true);
        settings.setJavaScriptEnabled(true);
        settings.setBlockNetworkImage(false);//解决图片不显示

        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
        settings.setSupportZoom(true);
        settings.setBuiltInZoomControls(true);// 设置WebView可触摸放大缩小
        settings.setUseWideViewPort(true);


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        // 允许文件访问
        settings.setAllowFileAccess(true);
        //请求加载百度，并交由Webclient去处理
//
        //使用WebViewClient设置监听并处理WebView的请求事件
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView webView, String url) {
                webView.loadUrl(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView webView, String s) {
                super.onPageFinished(webView, s);
                Log.d("url-finish", s);
            }
        });
        windowManager = getWindowManager();
        webView.setWebChromeClient(new com.tencent.smtt.sdk.WebChromeClient() {

            @Override
            public void onHideCustomView() {
                windowManager.removeViewImmediate(fullScreenLayer);
                fullScreenLayer = null;
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            }

            @Override
            public void onShowCustomView(View view, IX5WebChromeClient.CustomViewCallback customViewCallback) {
                windowManager.addView(view, new WindowManager.LayoutParams(WindowManager.LayoutParams.TYPE_APPLICATION));
                fullScreen(view);
                fullScreenLayer = view;
            }
        });
        webView.addJavascriptInterface(new MainJavascriptInterface(webView), "android");
//        webView.loadUrl("http://video.yto56test.com/?source=JSC&themeColor=DF7A3D&accessToken=a451a4038be84da98a298e50475bb8b7&userName=王凯&empNo=02231406&userOrgCode=431906&userOrgType=REGION_MANAGE&userOrgName=吉林区");
        //        webView.loadUrl("https://www.baidu.com");
//        webView.loadUrl("https://v4.21tb.com/wx/loginOut.do");
        webView.loadUrl("file:///android_asset/demo.html");
        Log.d("url", webView.getUrl());
//
    }

    private void RegisterMethod() {
        webView.registerHandler("saveHistory", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                Log.d("saveHistory", data.replace("\"", "").toString());
                function.onCallBack(data);
            }
        });

        webView.registerHandler("getData", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                Log.d("getData", data);
                function.onCallBack(data);
            }
        });

        webView.addJavascriptInterface(new MainJavascriptInterface(webView), "android");
        webView.sendToWeb("hello1");
    }

    private void fullScreen(View view) {
        view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
                | View.SYSTEM_UI_FLAG_FULLSCREEN
                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
    }

    @Override
    public void onBackPressed() {

        super.onBackPressed();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (webView != null && webView.canGoBack()) {
                webView.goBack();
            }
        }
        return super.onKeyDown(keyCode, event);
    }

}